import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'ProductBlock.dart';

String _title =  'Flutter电商demo';

class MyOnlineShop  extends StatelessWidget {
  const MyOnlineShop({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      theme: ThemeData(

        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: MyHomePage(title: _title),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});


  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  Map<int,Image> imageJar = {
    0: Image(image: NetworkImage('https://img11.360buyimg.com/n7/jfs/t1/41869/25/19988/62228/6350fcdaEa5779d9f/042845eb3f7170be.jpg')),
    1: Image(image: NetworkImage('https://img12.360buyimg.com/n7/jfs/t1/155691/24/31321/62656/635273aeE08e269cf/49519a13f72c01c8.jpg')),
    2: Image(image: NetworkImage('https://img10.360buyimg.com/n1/jfs/t1/168227/8/39212/86127/64ea66c1Fd116b5d4/2e98bf7172a57814.jpg')),
  };

  Image getImageFromJar (int id) {
    var get = imageJar[id];
    if (get != null) {
      return get;
    }
    return Image(image: new AssetImage('images/volume_dark.png'));
  }

  @override
  Widget build(BuildContext context) {
// 锁定屏幕方向
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp, // 仅允许纵向方向
    ]);
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Text(widget.title),
        ),
        body:
        ListView(children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
            ProductBlock(
                name: 'Apple iPad Pro12.9英寸(第6代)平板电脑2022年款(512GWLAN版/M2芯片/学习办工',
                id: '0', price: 11799.80, image: getImageFromJar(0)
            ),
            ProductBlock(
                name: 'Apple iPad Pro 外版 2022年款 11英寸 苹果ipad 平板电脑 M2芯片 22款iPad Pro11英寸 银色 128G wifi未使用【2年只换不修】',
                id: '1', price: 8480.09, image: getImageFromJar(1)),
            ProductBlock(
                name: '耐克（NIKE）JORDAN ULTRA FLY 2 LOW 男子篮球鞋 AH8110-114 42.5',
                id: '2', price: 420, image: getImageFromJar(2)),
          ],)
        ],)
    );
  }
}